﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;
using System.Windows.Data;

namespace HasuSLUI
{
    public partial class MetaGrid : UserControl
    {
        public MetaGrid()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MetaGrid_Loaded);
        }

        void MetaGrid_Loaded(object sender, RoutedEventArgs e)
        {
            this.TheGrid.LoadingRow += new EventHandler<DataGridRowEventArgs>(TheGrid_LoadingRow);
            
        }

        void TheGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            ;
        }

        public new object DataContext
        {
            get
            {
                return base.DataContext;
            }
            set 
            {
                base.DataContext = value;

                CreateColumns();

                if (base.DataContext is IEnumerable)
                {
                    this.TheGrid.ItemsSource = base.DataContext as IEnumerable;
                }
            }
        }

        protected void CreateColumns()
        {
            List<Metadata> metadataList = new List<Metadata>();
            object currentObj = null;
            if (DataContext.GetType().IsClass && DataContext is IEnumerable)
            {
                try
                {
                    IEnumerator itrat= (DataContext as IEnumerable).GetEnumerator();
                    if (itrat.MoveNext())
                    {
                        currentObj = itrat.Current;
                    }
                }
                catch (Exception ex)
                {
                    //throw;
                }
            }
            else if (DataContext.GetType().IsArray)
            {
                object[] objects = DataContext as object[];
                if (objects.Length > 0)
                {
                    currentObj = objects[0];
                }
            }
            if (currentObj == null)
            {
                return;
            }

            metadataList = MetadataProvider.GetVisibleMetadataCollection(currentObj);

            //var sortedMetadataList = metadataList.OrderBy(x => x.Group).ThenBy(x => x.Order);
            
            var visibleMetadataList = from m in  metadataList
                                     orderby m.Group, m.Order
                                     select m;

            this.TheGrid.Columns.Clear();
            this.TheGrid.AutoGenerateColumns = false;

            foreach (var metadata in visibleMetadataList)
            {
                DataGridColumn metacolumn = CreateColumn(metadata);
                this.TheGrid.Columns.Add(metacolumn); 
            }
        }

        protected DataGridColumn CreateColumn(Metadata metadata)
        {
            DataGridColumn column = null;

            column = UIDefaults.DefaultControlFactory.CreateColumn(metadata);

            if (column != null)
            {
                UIDefaults.DefaultControlFactory.SetBinding(column, metadata);
            }
            return column;
         }
    }
}
